package Year21_Month12.day1209;

public class Solution {
    public int shipWithinDays(int[] weights, int days) {
        int sumweight = 0;//用来记录重量总和
        int maxweight = 0;//用来记录最大重量
        for(int i:weights){
            sumweight += i;
            maxweight = Math.max(i,maxweight);
        }
        int left = maxweight;
        int right = sumweight;

        while(left < right){
            int mid = (left + right) / 2;
            if(check(weights,days,mid)){
                left = mid + 1;
            }else{
                right = mid;
            }
        }
        return left;
    }
    public boolean check(int[] weights,int days,int mid){
        int dayweight = 0;//用来记录一辆船上的重量
        int countday = 1;//用来记录所用天数
        for(int weight:weights){
            if(dayweight + weight > mid){
                dayweight = 0;
                countday++;
            }
            dayweight += weight;
        }
        return countday > days;
    }
}
